package Controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/smartwalle/alipay/v3"
	"net/url"
	"strconv"
	"web_back_go/Common"
	"web_back_go/DAO/gorm"
	"web_back_go/DAO/gorm/GormModel"
	"web_back_go/Dto"
	"web_back_go/Response"
	"web_back_go/Service"
	"web_back_go/Util"
)

/*
NormalPay
@Description: 根据订单号进行支付
*/
func NormalPay(c *gin.Context) {
	userId, _ := Common.GetUserId(c)
	payParam := &Dto.PayParam{}
	if err := Util.BindAndValidate(c, payParam); err != nil {
		c.JSON(200, Response.ResponseBody{
			Code:    "A00001",
			Data:    nil,
			Success: false,
			Msg:     "参数错误",
			Fail:    true,
		})
		return
	}

	var payInfo Dto.PayInfoDto = Service.Pay(userId, *payParam)
	// 根据内部订单号更新order settlement
	Service.PaySuccess(payInfo.PayNo, "")
	c.JSON(200, Response.ResponseBody{
		Code:    "00000",
		Data:    true,
		Success: true,
		Msg:     nil,
		Fail:    false,
	})
}

func Pay(c *gin.Context) {
	userId, _ := Common.GetUserId(c)
	payParam := &Dto.PayParam{}
	if err := Util.BindAndValidate(c, payParam); err != nil {
		c.JSON(200, Response.ResponseBody{
			Code:    "A00001",
			Data:    nil,
			Success: false,
			Msg:     "参数错误",
			Fail:    true,
		})
		return
	}
	var payInfo Dto.PayInfoDto = Service.Pay(userId, *payParam)
	Service.PaySuccess(payInfo.PayNo, "")
	c.JSON(200, Response.ResponseBody{
		Code:    "00000",
		Data:    nil,
		Success: true,
		Msg:     nil,
		Fail:    false,
	})
}

func ZFBPay(c *gin.Context) {
	userId, _ := Common.GetUserId(c)
	payParam := &Dto.PayParam{}
	if err := Util.BindAndValidate(c, payParam); err != nil {
		c.JSON(200, Response.ResponseBody{
			Code:    "A00001",
			Data:    nil,
			Success: false,
			Msg:     "参数错误",
			Fail:    true,
		})
		return
	}

	var payInfo Dto.PayInfoDto = Service.Pay(userId, *payParam)
	// 根据内部订单号更新order settlement
	Service.PaySuccess(payInfo.PayNo, "")
	//通过订单号查询订单编号
	db := gorm.Db
	var orderSettlement GormModel.TzOrderSettlement
	db.Where("pay_no = ?", payInfo.PayNo).First(&orderSettlement)
	appID := "2021000148622145"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              //你的appID
	privateKey := "MIIEpAIBAAKCAQEAs18h/cjqnGYxUBsom/9xIl+aLh+S0b9b0xH+O18Xzz5RtTfrAJPi8FBFcmCaprFDA+6Y4zvyBkK4qHCJaZYmbbButTegT7RRhaeqcIZhE5bxCHeoKhHFEdSfH3spKR8n/gTy7N0T4hNO1XU01JG1PGH6yPpRIUKGYGLgRJPHfw8Ek2o/41H6Sg5MRQ+CDWz7CbMq4oPoBx/Io/hB2xPuEG1NWMmiwm22lCVGl/D1Rm+EMHu1BSIv8mBc9cCuexkBndoto0tBnW3jv43Ou/aMzsAjJQ7ebaDTL9ZgehpxkIp9ayC5kk4sZZQSpjdBFTIHmAE02DEPbvS5iSyLEn8CFwIDAQABAoIBAEnu627U0sc2ZBsVpq5uIq69Yyqhvv2Pd4TPhM/qCUFV+Y3Nh6LMUh0lqQpVAUm2BN5pCUGYYtDrzXs4L5uC1UIfF/LAA+b2dye1DU6mRPszGxSXZYw5mNYZoqDyPewCOJS+a3bwRRLJTJQ1ZFo92MsTw6/1ZV1lM0glWTVLCE65KjQ4wEn6inxLkoVc5lTww2FmXx5AIiKopwExnY8/zpQ7LXLF3jqYc0PgT2Zkd/tnLBtEPq9qJOBEOZXA8NnOW0sgtff9cylnBCFnB2r9DyX1l8VULwFMioQr8T6ksh5sIJWqV5cnAj9KdHyo9zEKcDLjxSKjx4T/q8m7e7nIvqECgYEA+ffa6fpE0YDyMTxFlh4XrbpJ80sW5qaX14ArZ+j2AaT6vHHfEAnJnCC1wjddYcMxX2hoDMXVyCyYmtYHNha03LGM+CXgjccDmVrCcaMy9v2J8PfaZVn/7+Ub0Xh0F769N3l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43Ou/aMzsAjJQ7ebaDTL9ZgehpxkIp9ayC5kk4sZZQSpjdBFTIHmAE02DEPbvS5iSyLEn8CFwIDAQAB"

	var client, err = alipay.New(appID, privateKey, false)
	if err != nil {
		panic(err)
	}
	err = client.LoadAliPayPublicKey(aliPhublicKey)
	if err != nil {
		panic(err)
	}
	//var p = alipay.TradeWapPay{}
	var p = alipay.TradePagePay{}
	p.NotifyURL = "https://www.baidu.com"                                                                           //需要公网IP，没有支付宝回调
	p.ReturnURL = "http://localhost/pages/pay-result/pay-result?sts=1&orderNumbers=" + *orderSettlement.OrderNumber //支付后调转页面
	p.Subject = "区块链商店模拟支付"                                                                                         //标题
	p.OutTradeNo = *orderSettlement.OrderNumber                                                                     //传递一个唯一单号
	p.TotalAmount = strconv.FormatFloat(payInfo.PayAmount, 'f', 2, 64)                                              //金额
	//p.ProductCode = "QUICK_MSECURITY_PAY"
	p.ProductCode = "FAST_INSTANT_TRADE_PAY" //网页支付
	var url2 *url.URL
	url2, err = client.TradePagePay(p)
	if err != nil {
		fmt.Println(err)
	}

	var payURL = url2.String()
	fmt.Println("支付url是：" + payURL)
	c.JSON(200, Response.ResponseBody{
		Code:    "00000",
		Data:    payURL,
		Success: true,
		Msg:     nil,
		Fail:    false,
	})
}
